========================================================================= 
Log Path: ./log/logexternal.log 
Program Path: /Users/yorke/Dropbox/dissertation/3_Empirics/2_Code/vote_mobilization_code/replication/04_rep_external.R 
Working Directory: /Users/yorke/Dropbox/dissertation/3_Empirics/2_Code/vote_mobilization_code/replication 
User Name: yorke 
R Version: 4.3.1 (2023-06-16) 
Machine: AS23C1Q05N arm64 
Operating System: Darwin 21.6.0 Darwin Kernel Version 21.6.0: Mon Aug 22 20:20:05 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T8101 
Base Packages: stats graphics grDevices utils datasets methods base 
Other Packages: tidylog_1.1.0 logr_1.3.8 common_1.1.3 sandwich_3.0-2 stargazer_5.2.3 lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1 dplyr_1.1.4 purrr_1.0.2 readr_2.1.5 tidyr_1.3.1 tibble_3.2.1 ggplot2_3.5.1 tidyverse_2.0.0 
Log Start Time: 2024-06-03 16:48:33.86883 
========================================================================= 

> #### Replication Code for:
> #### Constituency Service and Electoral Accountability in Autocratic Legislatures
> #### Author: Erin York
> #### Created: 5/30/2024
> 
> # this code replicates Figure A4 and Table A15 in the supplementary appendix
> # this code requires access to geocoded Morocco Round 6 Afrobarometer data
> # see READ ME file for instructions on how to access this dataset
> 
> rm(list = ls())
> 
> ## Load Packages
> library(tidyverse)
> library(stargazer)
> library(sandwich)
> 
> library(logr)
> 
> options("logr.autolog" = TRUE)
> 
> # Open the log
> lf <- log_open(file.path("logexternal.log"))
> 
> # Send message to log
> log_code()
> 
> 
> 
> # read in data ------------------------------------------------------------
> 
> # afrobarometer data - adjust filepath as necessary
> afro_r6<- read_csv("MOR R6 Geocoded_fixed.csv")
> 
> 
> # geolink for afrobarometer + census indicators
> mergedat<- read_csv("df_afrolink.csv")
> 
> 
> 
> 
> # recode afrobarometer variables ------------------------------------------
> 
> afro_subset<- afro_r6 %>%
>   group_by(respno) %>%
>   # demographic controls
>   rename(age = q1) %>% # respondent age
>   mutate(poverty_hunger = as.numeric(q8b > 0), # gone without enough food to eat in past year
>          socialmedia = as.numeric(q12e > 0), # coded 1 for sometimes get news from social media
>          voted_2011 = as.numeric(q21 == 1), # coded 1 for voted in november 2011
>          female = as.numeric(q101 == 2), # coded 1 for women
>          edu_full = ifelse(q97 > 10, NA, q97), # education on 9 pt scale none to postgrad
>          reli_full = ifelse(q98b > 7|q98b < 0, NA, q98b), # religious practice from never to more than once/day
>          unemployed = as.numeric(q95 == 1), # respondent has no job and is looking for work
>          rifi = as.numeric(q87 == 1501), # identifies as rifi
>          soussi = as.numeric(q87 == 1502), # identifies as soussi
>          chalh = as.numeric(q87 == 1503), # identifies as chalh
>          sahraoui = as.numeric(q87 == 1504)) %>% # identifies as sahraoui
>   ## imputed controls (median value)
>   mutate(reli_full_imp = ifelse(is.na(reli_full), 3, reli_full),
>          edu_full_imp = ifelse(is.na(edu_full), 4, edu_full),
>          no_internet = as.numeric(q92b == 0)) %>% # never use internet
>   # political attitudes
>   ## political leanings
>   mutate(vote_opp = ifelse(q99 %in% c(1500, 1501, 1504, 1507), 1, 0),
>          vote_roy = ifelse(q99 %in% c(1502, 1503, 1505, 1506), 1, 0),
>          close_party = as.numeric(q90a == 1)) %>% # respondent feels close to a political party
>   ## government approval/responsiveness/corruption
>   mutate(mun_approve = ifelse(q68c == 9, NA, q68c), # approves of performance (4pt scale) in last 12 months
>          mp_approve = ifelse(q68b == 9, NA, q68b),
>          mp_listen = ifelse(q59a == 9, NA, q59a), # how often does X listen to ppl like you?
>          mun_listen = ifelse(q59b == 9, NA, q59b),
>          mp_corrupt = ifelse(q53b > 4, NA, q53b), 
>          mun_corrupt = ifelse(q53d > 4, NA, q53d)
>          ) %>%
>   ungroup() %>%
>   dplyr::select(respno, region, locationlevel1, locationlevel2, townvill,
>          voted_2011, no_internet, age, poverty_hunger, urbrur, socialmedia,
>          unemployed, rifi, sahraoui, chalh, soussi,
>          reli_full_imp, edu_full_imp, female,
>          vote_roy, vote_opp,
>          mp_corrupt, mun_corrupt,
>          mp_approve, mun_approve,
>          mp_listen, mun_listen) %>%
>   group_by(respno) %>%
>   mutate(votechoice = ifelse(vote_roy + vote_opp == 0, "No Party", ifelse(vote_roy == 1, "Royalist",
>                                                                           "Opposition")),
>          amazigh = as.numeric(rifi + sahraoui + chalh + soussi > 0),
>          reli_never = as.numeric(reli_full_imp == 0), # never practice religion
>          reli_often = as.numeric(reli_full_imp > 3), # practice religion more than 1x per week
>          edu_low = as.numeric(edu_full_imp < 4), # primary school or less
>          edu_high = as.numeric(edu_full_imp > 5)) %>%
>   mutate(gov_corrupt = mean(c(mp_corrupt, mun_corrupt), na.rm = T),
>          gov_approve = mean(c(mp_approve, mun_approve), na.rm = T),
>          gov_listen = mean(c(mp_listen, mun_listen), na.rm = T)) %>%
>   ungroup() %>%
>   mutate(age_std = scale(age),
>          corrupt_std = scale(gov_corrupt),
>          approve_std = scale(gov_approve),
>          listen_std = scale(gov_listen))
> 
> 
> # merge with census and activity data -------------------------------------
> 
> afro_full<- afro_subset %>% left_join(mergedat) 
> 
> # figure a4 ---------------------------------------------------------------
> 
> roy_pref<- lm(vote_roy ~ age_std + poverty_hunger + urbrur + socialmedia + unemployed +
>                 amazigh + reli_never + reli_often + edu_low + edu_high, 
>               filter(afro_full, votechoice != "No Party"))
> 
> fa4<- data_frame(x = coef(roy_pref), xlow = confint(roy_pref)[,1], xhi = confint(roy_pref)[,2],
>                  var = names(coef(roy_pref))) %>%
>   filter(var != "(Intercept)") %>%
>   mutate(Attribute = c("Age", "Poor", "Rural", "Uses Social Media",
>                        "Unemployed", "Amazigh", "Religion - never", "Religion - often",
>                        "Education - low", "Education - high")) %>%
>   mutate(attribute = reorder(Attribute, -x)) %>%
>   ggplot(aes(x = attribute)) +
>   geom_hline(aes(yintercept = 0), color = "Gray") +
>   geom_pointrange(aes(y = x, ymin = xlow, ymax = xhi)) +
>   coord_flip() +
>   theme_bw() + 
>   ylab("Prefer Royalist Party") + 
>   xlab("") 
> fa4
> 
> ggsave(fa4, filename = "FigureA4.pdf",
>        width = 8, height = 5)
> 
> 
> 
> # table a15 ---------------------------------------------------------------
> 
> # gov approval
> app<- lm(approve_std ~ log(anyref + 1), afro_full)
> appc2<- lm(approve_std ~ log(anyref + 1) +
>              log(population)+ illiteracy + unemployment + rural_housing  +Darija +
>              voted_2011+ no_internet+ age_std + poverty_hunger + urbrur + socialmedia + 
>              unemployed + amazigh + reli_never + reli_often + 
>              edu_low + edu_high  +female, afro_full)
> 
> list<- lm(listen_std ~ log(anyref + 1), afro_full)
> listc2<- lm(listen_std ~ log(anyref + 1) +
>               log(population)+ illiteracy + unemployment + rural_housing  +Darija +
>               voted_2011+ no_internet+ age_std + poverty_hunger + urbrur + socialmedia + 
>               unemployed + amazigh + reli_never + reli_often + 
>               edu_low + edu_high +female, afro_full)
> 
> corr<- lm(corrupt_std ~ log(anyref + 1), afro_full)
> corrc2<- lm(corrupt_std ~ log(anyref + 1) +
>               log(population)+ illiteracy + unemployment + rural_housing  +Darija +
>               voted_2011+ no_internet+ age_std + poverty_hunger + urbrur + socialmedia + 
>               unemployed + amazigh + reli_never + reli_often + 
>               edu_low + edu_high  +female, afro_full)
> 
> sel<- sqrt(diag(vcovCL(list, cluster = afro_full$commcode)))
> selc2<- sqrt(diag(vcovCL(listc2, cluster = afro_full$commcode)))
> 
> sea<- sqrt(diag(vcovCL(app, cluster = afro_full$commcode)))
> seac2<- sqrt(diag(vcovCL(appc2, cluster = afro_full$commcode)))
> 
> sec<- sqrt(diag(vcovCL(corr, cluster = afro_full$commcode)))
> secc2<- sqrt(diag(vcovCL(corrc2, cluster = afro_full$commcode)))
> 
> sink("TableA15.tex")
> stargazer( app, appc2,list, listc2, corr, corrc2,
>            se = list(sea, seac2, sel, selc2, sec, secc2),
>            covariate.labels = c("Log Municipal References",
>                                 "Log population", "Illiteracy Rate", "Unemployment Rate", "Prop. Rural Housing","Prop. Darija",
>                                 "Voted in 2011", "No Internet", "Age (standardized)", "Impoverished", "Rural", "Social Media User",
>                                 "Unemployed", "Amazigh", "Religion - never", "Religion - often",
>                                 "Education - low", "Education - high", "Female"),
>            dep.var.labels = c("Gov. Approval", "Gov. Listens", "Gov. Corrupt"),
>            omit.stat = c("ser", "f"),
>            no.space = T)
> sink()
> 
> ####
> log_close()

group_by: one grouping variable (respno)

NOTE: Log Print Time:  2024-06-03 16:48:35.798039 
NOTE: Elapsed Time: 1.92527103424072 secs 

rename: renamed one variable (age)

NOTE: Log Print Time:  2024-06-03 16:48:35.812266 
NOTE: Elapsed Time: 0.0142271518707275 secs 

mutate (grouped): new variable 'poverty_hunger' (double) with 2 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:35.906737 
NOTE: Elapsed Time: 0.0944709777832031 secs 

                  new variable 'socialmedia' (double) with 2 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:35.91849 
NOTE: Elapsed Time: 0.0117528438568115 secs 

                  new variable 'voted_2011' (double) with 2 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:35.930051 
NOTE: Elapsed Time: 0.0115611553192139 secs 

                  new variable 'female' (double) with 2 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:35.943256 
NOTE: Elapsed Time: 0.01320481300354 secs 

                  new variable 'edu_full' (double) with 11 unique values and <1% NA

NOTE: Log Print Time:  2024-06-03 16:48:35.96583 
NOTE: Elapsed Time: 0.0225741863250732 secs 

                  new variable 'reli_full' (double) with 8 unique values and 2% NA

NOTE: Log Print Time:  2024-06-03 16:48:35.99761 
NOTE: Elapsed Time: 0.0317800045013428 secs 

                  new variable 'unemployed' (double) with 2 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.005325 
NOTE: Elapsed Time: 0.00771498680114746 secs 

                  new variable 'rifi' (double) with 2 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.023184 
NOTE: Elapsed Time: 0.0178589820861816 secs 

                  new variable 'soussi' (double) with 2 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.032289 
NOTE: Elapsed Time: 0.00910496711730957 secs 

                  new variable 'chalh' (double) with 2 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.039044 
NOTE: Elapsed Time: 0.00675487518310547 secs 

                  new variable 'sahraoui' (double) with 2 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.043874 
NOTE: Elapsed Time: 0.00483012199401855 secs 

mutate (grouped): new variable 'reli_full_imp' (double) with 7 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.077759 
NOTE: Elapsed Time: 0.0338850021362305 secs 

                  new variable 'edu_full_imp' (double) with 10 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.086045 
NOTE: Elapsed Time: 0.0082859992980957 secs 

                  new variable 'no_internet' (double) with 2 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.096796 
NOTE: Elapsed Time: 0.0107510089874268 secs 

mutate (grouped): new variable 'vote_opp' (double) with 2 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.149811 
NOTE: Elapsed Time: 0.0530149936676025 secs 

                  new variable 'vote_roy' (double) with 2 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.16592 
NOTE: Elapsed Time: 0.0161089897155762 secs 

                  new variable 'close_party' (double) with 2 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.175363 
NOTE: Elapsed Time: 0.00944304466247559 secs 

mutate (grouped): new variable 'mun_approve' (double) with 5 unique values and 12% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.226512 
NOTE: Elapsed Time: 0.0511488914489746 secs 

                  new variable 'mp_approve' (double) with 5 unique values and 17% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.238097 
NOTE: Elapsed Time: 0.011584997177124 secs 

                  new variable 'mp_listen' (double) with 5 unique values and 11% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.252894 
NOTE: Elapsed Time: 0.0147969722747803 secs 

                  new variable 'mun_listen' (double) with 5 unique values and 8% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.281432 
NOTE: Elapsed Time: 0.0285379886627197 secs 

                  new variable 'mp_corrupt' (double) with 5 unique values and 13% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.28986 
NOTE: Elapsed Time: 0.00842809677124023 secs 

                  new variable 'mun_corrupt' (double) with 5 unique values and 8% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.320679 
NOTE: Elapsed Time: 0.0308189392089844 secs 

ungroup: no grouping variables remain

NOTE: Log Print Time:  2024-06-03 16:48:36.329199 
NOTE: Elapsed Time: 0.00852012634277344 secs 

group_by: one grouping variable (respno)

NOTE: Log Print Time:  2024-06-03 16:48:36.362581 
NOTE: Elapsed Time: 0.0333819389343262 secs 

mutate (grouped): new variable 'votechoice' (character) with 3 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.398264 
NOTE: Elapsed Time: 0.0356829166412354 secs 

                  new variable 'amazigh' (double) with 2 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.423665 
NOTE: Elapsed Time: 0.0254011154174805 secs 

                  new variable 'reli_never' (double) with 2 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.434512 
NOTE: Elapsed Time: 0.0108468532562256 secs 

                  new variable 'reli_often' (double) with 2 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.460909 
NOTE: Elapsed Time: 0.0263969898223877 secs 

                  new variable 'edu_low' (double) with 2 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.490814 
NOTE: Elapsed Time: 0.0299050807952881 secs 

                  new variable 'edu_high' (double) with 2 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.502822 
NOTE: Elapsed Time: 0.0120079517364502 secs 

mutate (grouped): new variable 'gov_corrupt' (double) with 8 unique values and 7% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.5527 
NOTE: Elapsed Time: 0.0498781204223633 secs 

                  new variable 'gov_approve' (double) with 8 unique values and 11% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.56142 
NOTE: Elapsed Time: 0.00871992111206055 secs 

                  new variable 'gov_listen' (double) with 8 unique values and 8% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.568102 
NOTE: Elapsed Time: 0.00668191909790039 secs 

ungroup: no grouping variables remain

NOTE: Log Print Time:  2024-06-03 16:48:36.57863 
NOTE: Elapsed Time: 0.0105280876159668 secs 

mutate: new variable 'age_std' (double) with 60 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.594305 
NOTE: Elapsed Time: 0.0156750679016113 secs 

        new variable 'corrupt_std' (double) with 8 unique values and 7% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.605689 
NOTE: Elapsed Time: 0.0113840103149414 secs 

        new variable 'approve_std' (double) with 8 unique values and 11% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.618841 
NOTE: Elapsed Time: 0.0131518840789795 secs 

        new variable 'listen_std' (double) with 8 unique values and 8% NA

NOTE: Log Print Time:  2024-06-03 16:48:36.644838 
NOTE: Elapsed Time: 0.0259971618652344 secs 

left_join: added 8 columns (commcode, illiteracy, unemployment, rural_housing, Darija, …)

NOTE: Log Print Time:  2024-06-03 16:48:36.703276 
NOTE: Elapsed Time: 0.0584378242492676 secs 

           > rows only in x             0

NOTE: Log Print Time:  2024-06-03 16:48:36.749685 
NOTE: Elapsed Time: 0.0464091300964355 secs 

           > rows only in mergedat (    0)

NOTE: Log Print Time:  2024-06-03 16:48:36.756207 
NOTE: Elapsed Time: 0.00652194023132324 secs 

           > matched rows           1,200

NOTE: Log Print Time:  2024-06-03 16:48:36.762042 
NOTE: Elapsed Time: 0.00583505630493164 secs 

           >                       =======

NOTE: Log Print Time:  2024-06-03 16:48:36.769925 
NOTE: Elapsed Time: 0.00788307189941406 secs 

           > rows total             1,200

NOTE: Log Print Time:  2024-06-03 16:48:36.779453 
NOTE: Elapsed Time: 0.00952792167663574 secs 

filter: removed 648 rows (54%), 552 rows remaining

NOTE: Log Print Time:  2024-06-03 16:48:36.818189 
NOTE: Elapsed Time: 0.0387358665466309 secs 

filter: removed one row (9%), 10 rows remaining

NOTE: Log Print Time:  2024-06-03 16:48:37.025203 
NOTE: Elapsed Time: 0.207014083862305 secs 

mutate: new variable 'Attribute' (character) with 10 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:37.035421 
NOTE: Elapsed Time: 0.0102179050445557 secs 

mutate: new variable 'attribute' (factor) with 10 unique values and 0% NA

NOTE: Log Print Time:  2024-06-03 16:48:37.046008 
NOTE: Elapsed Time: 0.010587215423584 secs 

Warning: length of NULL cannot be changed 

NOTE: Log Print Time:  2024-06-03 16:48:38.183543 
NOTE: Elapsed Time: 1.13753485679626 secs 

Warning: length of NULL cannot be changed 

NOTE: Log Print Time:  2024-06-03 16:48:38.194917 
NOTE: Elapsed Time: 0.0113739967346191 secs 

Warning: length of NULL cannot be changed 

NOTE: Log Print Time:  2024-06-03 16:48:38.209261 
NOTE: Elapsed Time: 0.0143439769744873 secs 

Warning: length of NULL cannot be changed 

NOTE: Log Print Time:  2024-06-03 16:48:38.216752 
NOTE: Elapsed Time: 0.00749111175537109 secs 

Warning: number of rows of result is not a multiple of vector length (arg 2) 

NOTE: Log Print Time:  2024-06-03 16:48:38.24106 
NOTE: Elapsed Time: 0.0243079662322998 secs 

Warning: number of rows of result is not a multiple of vector length (arg 2) 

NOTE: Log Print Time:  2024-06-03 16:48:38.336719 
NOTE: Elapsed Time: 0.0956590175628662 secs 

Warning: number of rows of result is not a multiple of vector length (arg 2) 

NOTE: Log Print Time:  2024-06-03 16:48:38.418722 
NOTE: Elapsed Time: 0.0820028781890869 secs 

Warning: length of NULL cannot be changed 

NOTE: Log Print Time:  2024-06-03 16:48:38.550637 
NOTE: Elapsed Time: 0.131915092468262 secs 

Warning: length of NULL cannot be changed 

NOTE: Log Print Time:  2024-06-03 16:48:38.558129 
NOTE: Elapsed Time: 0.0074920654296875 secs 

Warning: length of NULL cannot be changed 

NOTE: Log Print Time:  2024-06-03 16:48:38.570456 
NOTE: Elapsed Time: 0.0123269557952881 secs 

Warning: length of NULL cannot be changed 

NOTE: Log Print Time:  2024-06-03 16:48:38.58454 
NOTE: Elapsed Time: 0.0140838623046875 secs 

Warning: number of rows of result is not a multiple of vector length (arg 2) 

NOTE: Log Print Time:  2024-06-03 16:48:38.606955 
NOTE: Elapsed Time: 0.0224151611328125 secs 

Warning: number of rows of result is not a multiple of vector length (arg 2) 

NOTE: Log Print Time:  2024-06-03 16:48:38.712304 
NOTE: Elapsed Time: 0.105349063873291 secs 

Warning: number of rows of result is not a multiple of vector length (arg 2) 

NOTE: Log Print Time:  2024-06-03 16:48:38.815293 
NOTE: Elapsed Time: 0.102988958358765 secs 

========================================================================= 
Log End Time: 2024-06-03 16:48:39.2458 
Log Elapsed Time: 0 00:00:05 
========================================================================= 
